package comdawnling.kotlindemo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import java.util.*

class MainActivity : AppCompatActivity(), View.OnClickListener {
    val a1 = 4
    var a2 = 4
    private var tv: TextView? = null

    private val i: Int = 0
    private val mList = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv = findViewById<TextView>(R.id.tv)
        LogUtils.e("init1" + init1())
        LogUtils.e("a2\$a2")
        init()
        tv!!.setText("Hello World!")

        when (i) {
            0 -> {
            }
            1 -> {
            }
            else -> {
            }
        }

        for (i in mList.indices) {
            mList.add("item：$i")
        }

        //get()函数的作用
        val rectangle = Rectangle(41, 43)
        LogUtils.e(rectangle.isSquare)//结果：false
        rectangle.height = 43
        LogUtils.e(rectangle.isSquare)//结果：true

        //枚举类用法
        getWarmth(Color.RED)//结果：warm
        getWarmth(Color.BLUE)//结果：cold
    }

    fun init(){
        a2 = 0
        LogUtils.e("init$a2")
    }

    fun init1() = if (a1 > 0) a1 else 0

    fun init2(str: String) : String{
        return str
    }

    fun getWarmth(color : Color) = when(color){
        Color.BLUE, Color.INDIGO, Color.VIOLET -> "cold"
        Color.GREEN -> "neutral"
        Color.ORANGE, Color.YELLOW, Color.RED -> "warm"
    }

    //get()函数的作用
    class Rectangle(var height:Int, var width:Int){
        val isSquare: Boolean
        get() = height == width
    }

    enum class Color{
        RED, ORANGE, BLUE, YELLOW, GREEN, VIOLET, INDIGO
    }

    override fun onClick(v: View?) {
    }
}
